package your.packag;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;

import DAO.MyData;
import DTO.TransferDTO;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class CreateTransferActivity extends Activity implements OnClickListener {

	Spinner accSpinner;
	Spinner acctoSpinner;
	Spinner curSpinner;
	
	EditText money;
	
	
	Button create;
	
	
	
	TransferDTO transferDTO;
	DefaultHttpClient postClient;
	String valueacc,valueaccto,valuecur;
	List<Cookie> cookies;
	ProgressDialog dialog;
	 private Handler handler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	        	dialog.dismiss();
	        	LoadUI();
	        	

	        }
		};
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_transfer);
       
        postClient = LoginActivity.postClient;
        
        

        dialog = new ProgressDialog(this);
        dialog = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);
        new Thread(new Runnable() {
			
			  
				  public void run() {
					  LoadData();
					  handler.sendEmptyMessage(0);
				  }}).start();
	
        
        
		//accSpinner.setOnItemSelectedListener(this);
		//acctoSpinner.setOnItemSelectedListener(this);
		//curSpinner.setOnItemSelectedListener(this);
		
		
        
	}
	public void LoadUI()
	{
		 accSpinner = (Spinner)findViewById(R.id.createtransferspinner1);
	        acctoSpinner = (Spinner)findViewById(R.id.createtransferspinner3);
	        curSpinner = (Spinner)findViewById(R.id.createtransferspinner2);
	        
	        money = (EditText)findViewById(R.id.createtransfereditText1);
	       
	        
	        create = (Button)findViewById(R.id.createtransferbutton1);
	        ArrayAdapter<String> account = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, transferDTO.account());
			account.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			accSpinner.setAdapter(account);
			
			ArrayAdapter<String> accountto = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, transferDTO.accountto());
			account.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			acctoSpinner.setAdapter(accountto);
			

			ArrayAdapter<String> currency = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, transferDTO.currency());
			account.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			curSpinner.setAdapter(currency);
			
			create.setOnClickListener(this);
	}
	public void LoadData() 
	{
		
        
        String xml;
		try {
			/*HttpGet httpGet = new HttpGet("http://smartwebvina.com/smartlive/index.php?option=com_mymonney");
	        HttpResponse response = LoginActivity.postClient.execute(httpGet);
	        HttpEntity httpEntity =response.getEntity();
			xml = EntityUtils.toString(httpEntity);
			org.jsoup.nodes.Document doc = Jsoup.parse(xml);*/
			MyData data = new MyData();
		    transferDTO = data.readTransfer(FinancialActivity.doc);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		
		
	
	}
	public List<Cookie> CreateTransfer(String url,String acc,String accto,String money,String curr,String date) {

		
		   HttpPost httpPost = new HttpPost(url);
		   HttpResponse response;
		   List<Cookie> cookies = null;
		  
		

		   try {   
			  
		      // Make a List. Increase the size as you wish.
		      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		      // Add your form name and a text that belongs to the actual form.
		      
		     
		      nameValuePairs.add(new BasicNameValuePair("balance",money));
		      nameValuePairs.add(new BasicNameValuePair("currency", curr));
		      nameValuePairs.add(new BasicNameValuePair("danhmuc",valueacc));
		      nameValuePairs.add(new BasicNameValuePair("danhmuc2",valueaccto));

		
		    //  nameValuePairs.add(new BasicNameValuePair("remember", "yes"));
		      

		      nameValuePairs.add(new BasicNameValuePair("option", "com_mymonney"));  
		      nameValuePairs.add(new BasicNameValuePair("datec", "2012-08-07"));
		      nameValuePairs.add(new BasicNameValuePair("task", "transfer"));
		      nameValuePairs.add(new BasicNameValuePair("view", "parents"));
		     nameValuePairs.add(new BasicNameValuePair("f6f6cb580925b9a2a5f5d2e83482456e", "1"));

		      // Set the entity of your HttpPost.
		      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

		      // Execute your request against the given url and catch the response.
		      response = postClient.execute(httpPost);
		    
		    
		      // Status code 200 == successfully posted data.
		      if(response.getStatusLine().getStatusCode() == 200) {
		         // Green light. Catch your cookies from your HTTP response.
		          cookies =  postClient.getCookieStore().getCookies(); 
		         return  cookies;
		      }
		   } catch (Exception e) {
		   }
		   return  cookies;
	
	}
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		// TODO Auto-generated method stub
		/*switch(view.getId())
		{
			case R.id.createincomespinner1:
				String temp = (String) parent.getItemAtPosition(pos);
				for(int i =0 ;i< incomeDTO.account().size(); i++)
				{
					if(temp.compareTo(incomeDTO.account().get(i))==0)
					{
						valueacc = incomeDTO.valueacc().get(i);
					}
				}
				break;
				
			case R.id.createincomespinner2:
				temp = (String) parent.getItemAtPosition(pos);
				for(int i =0 ;i< incomeDTO.budget().size(); i++)
				{
					if(temp.compareTo(incomeDTO.budget().get(i))==0)
					{
						valuebud = incomeDTO.valuebud().get(i);
					}
				}
				break;
				
			case R.id.createincomespinner3:
			    temp = (String) parent.getItemAtPosition(pos);
				for(int i =0 ;i< incomeDTO.currency().size(); i++)
				{
					if(temp.compareTo(incomeDTO.currency().get(i))==0)
					{
						valuecur = incomeDTO.valuecur().get(i);
					}
				}
				break;
			
		
		}*/
		
		
		
	}
	public void onNothingSelected(AdapterView<?> arg0) {
		
	}
	public void onClick(View v) {
		switch(v.getId())
		{
		
		case R.id.createtransferbutton1:
			getValueSpinner();
			cookies = CreateTransfer("http://smartwebvina.com/smartlive/index.php?option=com_mymonney",
					valueacc,
					valueaccto,
					money.getText().toString(),		
					valuecur,
					""
					);
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Transfer Sucess")
		       .setCancelable(false)
		       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		               dialog.cancel();
		           }
		       });
			AlertDialog alert = builder.create();
			alert.show();
	
			break;
		}
		
	}
	
	public void getValueSpinner()
	{
		String temp = accSpinner.getSelectedItem().toString();
		for(int i =0 ;i< transferDTO.account().size(); i++)
		{
			if(temp.compareTo(transferDTO.account().get(i))==0)
			{
				valueacc = transferDTO.valueacc().get(i);
			}
		}
		
		temp = acctoSpinner.getSelectedItem().toString();
		
		for(int i =0 ;i< transferDTO.accountto().size(); i++)
		{
			if(temp.compareTo(transferDTO.accountto().get(i))==0)
			{
				valueaccto = transferDTO.valueaccto().get(i);
			}
		}
		
		
		temp = curSpinner.getSelectedItem().toString();
		
		for(int i =0 ;i< transferDTO.currency().size(); i++)
		{
			if(temp.compareTo(transferDTO.currency().get(i))==0)
			{
				valuecur = transferDTO.valuecur().get(i);
			}
		}
		
	
	}

}
